<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Appendix A. Implementing Actors</title>
    <link rel="stylesheet" href="style.css" type="text/css" />
    <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
    <link rel="start" href="index.html" title="Clutter编程" />
    <link rel="up" href="index.html" title="Clutter编程" />
    <link rel="prev" href="sec-full-example.html" title="Chapter 9. 一个完整的例子" />
    <link rel="next" href="custom-actor-example.html" title="Example" />
  </head>
  <body>
    <div class="navheader">
      <table width="100%" summary="Navigation header">
        <tr>
          <th colspan="3" align="center">Appendix A. Implementing Actors</th>
        </tr>
        <tr>
          <td width="20%" align="left"><a accesskey="p" href="sec-full-example.html"><img src="../icons/prev.png" alt="Prev" /></a> </td>
          <th width="60%" align="center"> </th>
          <td width="20%" align="right"> <a accesskey="n" href="custom-actor-example.html"><img src="../icons/next.png" alt="Next" /></a></td>
        </tr>
      </table>
      <hr />
    </div>
    <div class="appendix" lang="en" xml:lang="en">
      <div class="titlepage">
        <div>
          <div>
            <h2 class="title"><a id="appendix-implementing-actors"></a>Appendix A. Implementing Actors</h2>
          </div>
        </div>
      </div>
      <div class="toc">
        <p>
          <b>Table of Contents</b>
        </p>
        <ul>
          <li>
            <span class="sect1">
              <a href="appendix-implementing-actors.html#appendix-implementing-actors-simple">Implementing Simple Actors</a>
            </span>
          </li>
          <li>
            <span class="sect1">
              <a href="custom-actor-example.html">Example</a>
            </span>
          </li>
          <li>
            <span class="sect1">
              <a href="appendix-implementing-actors-containers.html">Implementing Container Actors</a>
            </span>
          </li>
          <li>
            <span class="sect1">
              <a href="custom-container-example.html">Example</a>
            </span>
          </li>
        </ul>
      </div>
      <div class="sect1" lang="en" xml:lang="en">
        <div class="titlepage">
          <div>
            <div>
              <h2 class="title" style="clear: both"><a id="appendix-implementing-actors-simple"></a>Implementing Simple Actors</h2>
            </div>
          </div>
        </div>
        <p>
If the standard <span class="application">Clutter</span> actors don't meet all your needs then you may create your own custom actor objects. 
Implementing a custom actor is much like implementing any new GObject type. You may use the <code class="function">G_DEFINE_TYPE</code> 
macro to specify that the type is derived from <code class="classname">ClutterActor</code>. For instance:
</p>
        <pre class="programlisting">
G_DEFINE_TYPE (ClutterTriangle, clutter_triangle, CLUTTER_TYPE_ACTOR);
</pre>
        <p>
</p>
        <p>You should then specify your object's implementation of the <code class="function">ClutterActor::paint()</code> virtual function in your <code class="literal">class_init</code> function: 
</p>
        <pre class="programlisting">
static void
clutter_triangle_class_init (ClutterTriangleClass *klass)
{
  ClutterActorClass *actor_class = CLUTTER_ACTOR_CLASS (klass);

  actor_class-&gt;paint = clutter_triangle_paint;

  ...
}
</pre>
        <p>
</p>
        <p>
Your <code class="function">ClutterActor::paint()</code> implementation should use the OpenGL API to actually paint something. 
You will probably need some information from your object's generic <code class="classname">ClutterActor</code> base class, for instance 
by calling <code class="function">clutter_actor_get_geometry()</code> and <code class="function">clutter_actor_get_opacity()</code>, and by using your object's specific property values. 
</p>
        <p>To make your code work with both OpenGL ES and regular OpenGL (and maybe even future <span class="application">Clutter</span> backends), you may wish 
to use <span class="application">Clutter</span>'s <code class="literal">cogl</code> abstraction API which provides functions such as <code class="function">cogl_rectangle()</code> 
and <code class="function">cogl_push_matrix()</code>. You can also detect whether the platform has support for either the OpenGL or OpenGL ES API by ifdefing for 
<code class="literal">CLUTTER_COGL_HAS_GL</code> or <code class="literal">CLUTTER_COGL_HAS_GLES</code>.
</p>
        <p>You should also implement the <code class="function">ClutterActor::pick()</code> virtual function, painting a silhouette of your actor 
in the provided color. <span class="application">Clutter</span> uses this to draw each actor's silhouette offscreen in a unique color, using the color to quickly 
identify the actor under the cursor. If your actor is simple then you can probably reuse the code from your paint() implementation.</p>
        <p>Most of the rest of <code class="classname">ClutterActor</code>'s virtual functions don't need to be reimplemented, because a suitable default implemention exists in 
<code class="classname">ClutterActor</code>. For instance, 
<code class="function">show()</code>, <code class="function">show_all()</code>, <code class="function">hide()</code>, <code class="function">hide_all()</code>, 
<code class="function">request_coord()</code>.

</p>
      </div>
    </div>
    <div class="navfooter">
      <hr />
      <table width="100%" summary="Navigation footer">
        <tr>
          <td width="40%" align="left"><a accesskey="p" href="sec-full-example.html"><img src="../icons/prev.png" alt="Prev" /></a> </td>
          <td width="20%" align="center"> </td>
          <td width="40%" align="right"> <a accesskey="n" href="custom-actor-example.html"><img src="../icons/next.png" alt="Next" /></a></td>
        </tr>
        <tr>
          <td width="40%" align="left" valign="top">Chapter 9. 一个完整的例子 </td>
          <td width="20%" align="center">
            <a accesskey="h" href="index.html">
              <img src="../icons/home.png" alt="Home" />
            </a>
          </td>
          <td width="40%" align="right" valign="top"> Example</td>
        </tr>
      </table>
    </div>
  </body>
</html>
